<template>
    <el-card style="width: 500px;">
        <el-form label-width="80px" size="small">
            <el-upload class="avatar-uploader" action="http://localhost:8081/common/upload" :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>

            <el-form-item label="用户名">
                <el-input v-model="form.username" disabled autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
                <el-input v-model="form.nickname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="form.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="擅长">
                <el-input type="textarea" disabled="" v-model="form.description" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save">确 定</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
//   import {serverIp} from "../../public/config";

export default {
    name: "Person",
    data() {
        return {
            // serverIp: serverIp,
            form: {},
            user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
            imageUrl: ''
        }
    },
    created() {
        //实例创建之后获取个人信息表单数据
        this.getUser().then(res => {
            console.log("当前的res" ,res)
            this.form = res
            this.imageUrl = "http://localhost:8081/common/download?name=" + res.image
        })
    },
    methods: {
        //异步获取用户信息
        async getUser() {
            return (await this.request.get("/user/username/" + this.user.username)).data
        },
        save() {
            this.request.post("/user", this.form).then(res => {
                if (res.code === '200') {
                    this.$message.success("保存成功")

            
                    //绑定事件 save方法执行时 Header同步更新头像数据
                    this.$bus.$emit("getUserImage",this.form)
                    // 更新浏览器存储的用户信息
                    this.getUser().then(res => {
                        res.token = JSON.parse(localStorage.getItem("user")).token
                        localStorage.setItem("user", JSON.stringify(res))
                    })

                } else {
                    this.$message.error("保存失败")
                }
            })
        },
        //图片上传成功回调函数
        handleAvatarSuccess(res) {
            this.imageUrl = "http://localhost:8081/common/download?name=" + res.msg//图片回显
            this.form.image = res.msg
        }
    }
}
</script>

<style>
.avatar-uploader {
    text-align: center;
    padding-bottom: 10px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 138px;
    height: 138px;
    line-height: 138px;
    text-align: center;
}

.avatar {
    width: 138px;
    height: 138px;
    display: block;
}
</style>
